package com.letter.otools.day.diary;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.letter.otools.day.R;
import com.letter.otools.day.listener.PositionListener;
import com.letter.otools.day.util.ImageUtils;
import com.letter.otools.day.util.PopupUtils;
import com.letter.otools.day.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.ViewHolder> {

    private List<Diary> diaryList;
    private PositionListener positionListener;

    public void setPositionListener(PositionListener positionListener) {
        this.positionListener = positionListener;
    }

    public DiaryAdapter(List<Diary> diaryList) {
        this.diaryList = diaryList;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle, tvDate, tvPreview, tvWordCount;
        public ImageView ivWeather, ivMood;
        public View tvTitleGap, imageLinView;
        public ImageView image1, image2, image3, image4;
        public View imageView1, imageView2, imageView3, imageView4;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvPreview = itemView.findViewById(R.id.tvPreview);
            tvWordCount = itemView.findViewById(R.id.tvWordCount);
            ivWeather = itemView.findViewById(R.id.ivWeather);
            ivMood = itemView.findViewById(R.id.ivMood);
            tvTitleGap = itemView.findViewById(R.id.tvTitleGap);
            image1 = itemView.findViewById(R.id.image1);
            imageView1 = itemView.findViewById(R.id.imageView1);
            image2 = itemView.findViewById(R.id.image2);
            imageView2 = itemView.findViewById(R.id.imageView2);
            image3 = itemView.findViewById(R.id.image3);
            imageView3 = itemView.findViewById(R.id.imageView3);
            image4 = itemView.findViewById(R.id.image4);
            imageView4 = itemView.findViewById(R.id.imageView4);
            imageLinView = itemView.findViewById(R.id.imageLinView);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.diary_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Diary diary = diaryList.get(position);

        holder.tvTitle.setText(diary.getTitle());
        holder.tvTitle.setVisibility(StringUtils.isEmpty(diary.getTitle()) ? View.GONE : View.VISIBLE);
        holder.tvTitleGap.setVisibility(holder.tvTitle.getVisibility());
        holder.tvDate.setText(TimeUtils.millis2String(diary.getCreateTime(), "HH:mm"));
        holder.tvPreview.setText(diary.getContent());
        holder.tvPreview.setVisibility(StringUtils.isEmpty(diary.getContent()) ? View.GONE : View.VISIBLE);
        if (StringUtils.isEmpty(diary.getTitle()) && !StringUtils.isEmpty(diary.getContent())) {
            holder.tvTitle.setText(diary.getContent());
            holder.tvTitle.setVisibility(View.VISIBLE);
            holder.tvTitleGap.setVisibility(View.GONE);
            holder.tvPreview.setVisibility(View.GONE);
        }
        String loadInvoicePic = diary.getImages();
        if (StringUtils.isNotEmpty(loadInvoicePic)) {
            List<ImageView> imageViewList = new ArrayList();
            imageViewList.add(holder.image1);
            imageViewList.add(holder.image2);
            imageViewList.add(holder.image3);
            imageViewList.add(holder.image4);
            holder.imageLinView.setVisibility(View.VISIBLE);
            List<String> imageList = Arrays.asList(loadInvoicePic.split(","));
            for (int i = 0; i < 4; i++) {
                final int fi = i;
                if (i < imageList.size() && StringUtils.isNotEmpty(imageList.get(i))) {
                    imageViewList.get(i).setVisibility(View.VISIBLE);
                    String imageUrl = imageList.get(i);
                    ImageUtils.showImage(imageViewList.get(i).getContext(), imageUrl, imageViewList.get(i));
                    imageViewList.get(i).setOnClickListener(v -> {
                        PopupUtils.preViewImages(imageViewList.get(fi).getContext(), imageViewList.get(fi), fi, imageList);
                    });
                } else {
                    imageViewList.get(i).setVisibility(View.INVISIBLE);
                }
            }
        } else {
            holder.imageLinView.setVisibility(View.GONE);
        }
//        holder.tvWordCount.setText(diary.getContent().length() + "字");

        // 设置天气图标
//        int weatherResId = getWeatherIcon(diary.getWeather());
//        holder.ivWeather.setImageResource(weatherResId);

        // 设置心情图标
//        int moodResId = getMoodIcon(diary.getMood());
//        holder.ivMood.setImageResource(moodResId);

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            // 打开日记详情
            positionListener.onPosition(1, diary);
        });
    }

//    private int getWeatherIcon(String weather) {
//        switch (weather) {
//            case "sunny": return R.drawable.ic_sunny;
//            case "cloudy": return R.drawable.ic_cloudy;
//            case "rainy": return R.drawable.ic_rainy;
//            default: return R.drawable.ic_unknown;
//        }
//    }
//
//    private int getMoodIcon(String mood) {
//        switch (mood) {
//            case "happy": return R.drawable.ic_happy;
//            case "normal": return R.drawable.ic_normal;
//            case "sad": return R.drawable.ic_sad;
//            default: return R.drawable.ic_unknown;
//        }
//    }

    @Override
    public int getItemCount() {
        return diaryList.size();
    }
}
